<template>
  <el-card>
    <el-form ref="form"
             label-width="120px"
             label-position="right"
             :model="formData"
             :rules="rules"
             size="small"
             style="width: 900px; min-width: 900px">
      <el-form-item label="公司编号"
                    prop="carrierTenantCode"
                    class="characterBox">
        <el-input v-model.trim="formData.carrierTenantCode"
                  placeholder="请输入公司编号"
                  @blur="handleBlur" />
      </el-form-item>
      <el-form-item label="公司全称"
                    prop="carrierName"
                    style="margin-bottom: 18px">
        <el-input placeholder="请输入公司全称"
                  disabled
                  :value="formData.carrierName" />
      </el-form-item>
      <el-form-item label="承运方简称"
                    prop="carrierAbbr"
                    style="margin-bottom: 18px">
        <el-input v-model.trim="formData.carrierAbbr"
                  placeholder="请输入承运方简称"
                  maxlength="20" />
      </el-form-item>
      <el-form-item label="委托对账"
                    prop="autoBalance"
                    class="characterBox">
        <Radio v-model="formData.autoBalance"
               :options="whetherOptions"
               :tip="tip"
               :tip-style="tipStyle" />
      </el-form-item>
      <el-form-item label="邀请信"
                    prop="welcome"
                    style="margin-bottom: 18px">
        <el-input v-model.trim="formData.welcome"
                  placeholder="请输入邀请信"
                  type="textarea"
                  maxlength="100" />
      </el-form-item>
      <el-form-item label=" ">
        <el-button @click="submit">发送</el-button>
        <el-button @click="$router.go(-1)">返回</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
import Radio from "@/components/advance/Radio";
import { ValidateRules } from "@/libs/util";
import { tenant } from "@/api/index";
import { whetherOptions } from "@/libs/constant.js";
export default {
  name: "AddCarriers",
  components: { Radio },
  data () {
    return {
      rules: {
        carrierTenantCode: [ValidateRules.carriersCodeBlur],
        carrierAbbr: [ValidateRules.carrierAbbrBlur],
      },
      formData: {
        welcome: "",
        carrierTenantCode: "",
        carrierName: "",
        carrierAbbr: "",
        autoBalance: null
      },
      whetherOptions,
      tip: "说明：开启委托对账后，承运方发送的对账单，本方不需要人工审核，自动通过",
      tipStyle: "margin-left: 20px;",
      details: {},
      code: ""
    };
  },
  methods: {
    async getTenantByCode () {
      if (!this.formData.carrierTenantCode) {
        console.log("租户编码不能为空")
        return
      }
      const response = await tenant.getTenantByCode(this.formData.carrierTenantCode);
      if (0 !== response?.code) {
        console.log("查询租户失败", response);
        this.formData.carrierTenantCode = null;
      }

      if (!response.data) {
        console.log("查询租户不存在", response);
        this.$message.warning('没有找到该公司编号，请核对或重新输入')
      }

      this.details = response.data
      this.code = response.data?.code

      this.formData.carrierTenantCode = this.code || this.formData.carrierTenantCode;
      this.formData.carrierName = this.details?.name;
      this.formData.carrierAbbr = this.details?.alias;
      if (this.formData.carrierName && !this.formData.carrierAbbr) {
        this.formData.carrierAbbr = this.formData.carrierName
      }
      if (this.formData.carrierTenantCode && this.formData.carrierName && this.formData.carrierAbbr) {
        this.$refs.form.validate();
      }

    },
    handleBlur () {
      this.getTenantByCode();
    },
    submit () {
      if (this.formData.carrierTenantCode != this.code) {
        this.$message.warning('没有找到该公司编号，请核对或重新输入')
        return
      }

      this.$refs.form.validate((valid) => {
        if (valid) {
          this.insert();
        }
      });
    },
    async insert () {
      let { code, message } = await tenant.insert(this.formData);
      this.$message[code === 0 ? "success" : "error"](message);
      if (code === 0) {
        this.$router.go(-1);
      }
    }
  }
};
</script>
